import java.util.*;

public class Test30 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        String s1 = in.next();
        String s2 = in.next();


        int[][] dp = new int[m + 1][m + 1];
        dp[0][0] = 0;
        for (int k = 0; k < m; k++) {
            int left1 = 0;
            for (int i = k; i < m; i++) {
                boolean bool = false;
                for (int j = left1; j < n; j++) {
                    if (s1.charAt(j) == s2.charAt(i)) {
                        dp[k + 1][i + 1] = dp[k + 1][i] + 1;
                        left1 = j;
                        bool = true;
                        break;
                    }
                }
                if (!bool)dp[k + 1][i + 1] = dp[k + 1][i];
            }
        }
        int max = 0;
        for (int i = 1; i <= m; i++) {
            max = Math.max(max, dp[i][m]);
        }
        System.out.println(max);
    }






    public static void main02(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            int k = in.nextInt();
            int[] a = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = in.nextInt();
            }

            Arrays.sort(a);

            int left = 0, right = n - 1;
            int max=findMax(a,left,right,k);
            System.out.println(max);
        }
    }

    public static int findMax(int[] arr, int left, int right, int k) {
        if( left >= right){
            return 0;
        }
        if (arr[right] - arr[left] > k) {
            int a = findMax(arr, left + 1, right, k);
            int b = findMax(arr, left, right-1, k);
            return a > b ? a : b;
        }
        return right-left+1;
    }







    public static void main01(String[] args) {
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        while (T-- != 0) {
            int a = in.nextInt();
            int b = in.nextInt();
            boolean bool=isPrime(a*b);
            if(bool){
                System.out.println("Yes");
            }else{
                System.out.println("NO");
            }
        }
    }

    public static boolean isPrime(int k){
        if(k == 1 || k== 2 || k==3){
            return true;
        }
        for(int i=2;i<=Math.sqrt(k);i++){
            if(k % i == 0){
                return false;
            }
        }
        return true;
    }
}
